/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

/*
 * Main class and GUI class
 */
public class Test1 extends JFrame {

    /**
     * The serialVersionUID is a universal version identifier for a Serializable
     * class. Deserialization uses this number to ensure that a loaded class
     * corresponds exactly to a serialized object. If no match is found, then an
     * InvalidClassException is thrown.
     */
    private static final long serialVersionUID = -7971926486878447781L;
    private JComboBox _jComboBox = new JComboBox();

    /*
     * Constructor
     */
    public Test1() {

        // Set frame location
        setLocation(new Point(200, 200));

        // Set default close operation
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        // Add widget to frame
        getContentPane().add(_jComboBox);

        // Sets the renderer that paints the list items and the item selected from the list in the JComboBox field.
        _jComboBox.setRenderer(new MultiColumnRender());

        // Add some items to the combo box
        _jComboBox.addItem(new String[]{"1", "Ovidiu", "CONEAC", "27"});
        _jComboBox.addItem(new String[]{"2", "John", "McKANE", "37"});
        _jComboBox.addItem(new String[]{"3", "Mandy", "TREVOR", "22"});
        _jComboBox.addItem(new String[]{"4", "Alex", "BLACK", "40"});
        _jComboBox.addItem(new String[]{"5", "Steve", "CARL", "23"});
        _jComboBox.addItem(new String[]{"6", "Lucia", "STEVENS", "17"});

        _jComboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                doSomething();
            }

            private void doSomething() {
                String[] object = (String[]) _jComboBox.getSelectedItem();
                System.out.println(object[0]);
                System.out.println(object[1]);
                System.out.println(object[2]);
                System.out.println(object[3]);

            }
        });

        // Causes this Window to be sized to fit the preferred size and layouts of its subcomponents.
        pack();
    }

    public static final void main(String[] args) {
        new Test1().setVisible(true);
    }
}

/*
 * Custom list cell render for our JComboBox
 */
class MultiColumnRender implements ListCellRenderer {

    private JPanel _panelListCell = new JPanel();
    /**
     * The serialVersionUID is a universal version identifier for a Serializable
     * class. Deserialization uses this number to ensure that a loaded class
     * corresponds exactly to a serialized object. If no match is found, then an
     * InvalidClassException is thrown.
     */
    private static final long serialVersionUID = -722402815727238028L;
    private JLabel _lableComponentColumn1 = new JLabel();
    private JLabel _lableComponentColumn2 = new JLabel();
    private JLabel _lableComponentColumn3 = new JLabel();
    private JLabel _lableComponentColumn4 = new JLabel();

    /*
     * Constructor
     */
    public MultiColumnRender() {

        // Set layout
        GridLayout gridLayout = new GridLayout(
                1, // rows 
                4 // columns
                );
        _panelListCell.setLayout(gridLayout);

        // Add components
        _panelListCell.add(_lableComponentColumn1);
        _panelListCell.add(_lableComponentColumn2);
        _panelListCell.add(_lableComponentColumn3);
        _panelListCell.add(_lableComponentColumn4);

        // Add column 1 content type
        _panelListCell.add(_lableComponentColumn1);

        // Add column 2 content type
        _panelListCell.add(_lableComponentColumn2);

        // Add column 3 content type
        _panelListCell.add(_lableComponentColumn3);

        // Add column 4 content type
        _panelListCell.add(_lableComponentColumn4);
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

        String[] values = (String[]) value;

        _lableComponentColumn1.setText(values[0]);
        _lableComponentColumn2.setText(values[1]);
        _lableComponentColumn3.setText(values[2]);
        _lableComponentColumn4.setText(values[3]);

        _lableComponentColumn1.setOpaque(true);
        _lableComponentColumn2.setOpaque(true);
        _lableComponentColumn3.setOpaque(true);
        _lableComponentColumn4.setOpaque(true);

        if (index % 2 == 0) {
            _lableComponentColumn1.setBackground(new Color(255, 255, 204)); // FFFFCC
            _lableComponentColumn2.setBackground(new Color(255, 255, 204)); // FFFFCC
            _lableComponentColumn3.setBackground(new Color(255, 255, 204)); // FFFFCC
            _lableComponentColumn4.setBackground(new Color(255, 255, 204)); // FFFFCC
        } else {
            _lableComponentColumn1.setBackground(new Color(255, 255, 255)); // FFFFCC
            _lableComponentColumn2.setBackground(new Color(255, 255, 255)); // FFFFCC
            _lableComponentColumn3.setBackground(new Color(255, 255, 255)); // FFFFCC
            _lableComponentColumn4.setBackground(new Color(255, 255, 255)); // FFFFCC
        }

        return _panelListCell;
    }
}